var ws, aid, uid, pinglun;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	aid = ws.aid;
	uid = plus.storage.getItem('uid');
	var ndata = plus.storage.getItem('liuyan');
	var datas = $.parseJSON(ndata);
	if(datas){
		var list = '';
		$.each(datas, function(k,v) {
			if(v.aid == aid){
//				console.log(JSON.stringify(v));
				$('.pic').attr('src',getAvatar(v.avatar));
				$('.name').text(v.name);
				$('.ftime').text(v.addtime);
				$('.cont').text(v.content);
			}
		});
	}
	pinglist();
	//发表评论 
	$('.mui-btn').click(function(){
		console.log($('.pinglun').attr('data-name'));//回复的那条评论的用户Id
		var hui  = $('.pinglun').attr('data-name');
		var ping = $('.pinglun').val();
		if(!ping.trim()){
			toast('评论内容不能为空');return;
		}
		if(hui == undefined || !hui){
			var huifu = "wu";
		}else{
			var huifu = hui;
		}
		console.log(huifu);
//		console.log(apiRoot+"?m=home&c=bbs&a=addAdvice&aid="+aid+"&uid="+uid+"&cont="+ping+"&huifu="+huifu);
		showWating();
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=bbs&a=addAdvice&aid="+aid+"&uid="+uid+"&cont="+ping+"&huifu="+huifu,
			dataType :'json',
			success : function(data){
				console.log(JSON.stringify(data));
				plus.nativeUI.closeWaiting();
				if(data > 0){//评论成功
					toast('评论成功');
					$('.pinglun').val('');
					$('.pinglun').attr('data-name','');
					pinglun++;//评论增加
					$('.pinglun').text(pinglun);
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
},false)
function pinglist(){
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=bbs&a=oneliu&aid="+aid,
		success :function(data){
			plus.storage.setItem('pdata',data);
			var da = $.parseJSON(data);
//			console.log(JSON.stringify(da));  
			(da)?pinglun = parseInt(da.zongp):pinglun = 0;
			$('.zongnum').text(pinglun);
			if(da && da.review){
				var list = '';
				$.each(da.review, function(key,value) {
					plus.nativeUI.closeWaiting();
	//				console.log(JSON.stringify(value));
					list += '<div class="mui-table-view table-view-cell bbs-ico huifu" data-name="'+value.name+'">'+
								'<div class="overflow-h">'+
									'<i><img src="'+getAvatar(value.avatar)+'"/></i>'+
									'<div>'+
										'<li>';
										if(value.huifu){
											list += '<span>'+value.name+' 回复  '+value.huifu+'</span>';
										}else{
											list += '<span>'+value.name+'</span>';
										}
	//												'<span>'+(value.huifu)?(value.name+' 回复'+value.huifu):(value.name)+'</span>'+
									list +=  '<span class="mui-h6 mui-pull-right">'+value.time+'</span>'+
										'</li>'+
										'<li class="mui-h6">'+value.cont+'</li>'+
									'</div>'+
								'</div>'+
							'</div>';
				});
				$('.liulist').html(list);
				$('.huifu').click(function(){
					console.log($(this).attr('data-name'));
					$('.pinglun').attr('data-name',$(this).attr('data-name'));
					$('.pinglun').focus();
				})
			}else{
				plus.nativeUI.closeWaiting();
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			var data = plus.storage.getItem('pdata');
			var da = $.parseJSON(data);
//			console.log(JSON.stringify(da));  
			pinglun = parseInt(da.zongp);
			$('.zongnum').text(pinglun);
			if(da.review){
				var list = '';
				$.each(da.review, function(key,value) {
					plus.nativeUI.closeWaiting();
	//				console.log(JSON.stringify(value));
					list += '<div class="mui-table-view table-view-cell bbs-ico huifu" data-name="'+value.name+'">'+
								'<div class="overflow-h">'+
									'<i><img src="'+getAvatar(value.avatar)+'"/></i>'+
									'<div>'+
										'<li>';
										if(value.huifu){
											list += '<span>'+value.name+' 回复  '+value.huifu+'</span>';
										}else{
											list += '<span>'+value.name+'</span>';
										}
	//												'<span>'+(value.huifu)?(value.name+' 回复'+value.huifu):(value.name)+'</span>'+
									list +=  '<span class="mui-h6 mui-pull-right">'+value.time+'</span>'+
										'</li>'+
										'<li class="mui-h6">'+value.cont+'</li>'+
									'</div>'+
								'</div>'+
							'</div>';
				});
				$('.liulist').html(list);
				$('.huifu').click(function(){
					console.log($(this).attr('data-name'));
					$('.pinglun').attr('data-name',$(this).attr('data-name'));
					$('.pinglun').focus();
				})
			}
		}
	});
}
var tt = setInterval(pinglist,3000);